﻿#region LGPL License

/*
OpenSearchNet
Copyright (c) 2014, Brad Hards <bradh@frogmouth.net>, All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.
*/

#endregion LGPL License

using System.Collections.Generic;
using System.Linq;

namespace OpenSearch
{
    /// <summary>
    /// One query parameter with associated namespace
    /// </summary>
    /// <remarks>
    /// <para>
    /// OpenSearch query parameters can appear in the OpenSearch namespace, or they can be provided in
    /// extension namespaces (including private extensions). A query parameter in the OpenSearch namespace
    /// is known as a local parameter. Query parameters in other namespaces are known as extension parameters.
    /// </para>
    /// <para>
    /// The local parameters are:
    /// <list type="">
    /// <item>searchTerms</item>
    /// <item>count</item>
    /// <item>startIndex</item>
    /// <item>startPage</item>
    /// <item>language</item>
    /// <item>inputEncoding</item>
    /// <item>outputEncoding</item>
    /// </list>
    /// </para>
    /// </remarks>
    public class NameSpacedParameter
    {
        private string parameterNamespace;
        private string parameterName;

        internal NameSpacedParameter(string parameterWithImplicitOrExplicitNamespace, IDictionary<string, string> namespaces, string defaultNamespaceUri)
        {
            if (parameterWithImplicitOrExplicitNamespace.Contains(':'))
            {
                // has explicit namespace
                string[] splitParameter = parameterWithImplicitOrExplicitNamespace.Split(':');
                parameterName = splitParameter[1];
                string nsPrefix = splitParameter[0];
                parameterNamespace = namespaces[nsPrefix];
            }
            else
            {
                // implicit namespace
                parameterName = parameterWithImplicitOrExplicitNamespace;
                parameterNamespace = defaultNamespaceUri;
            }
        }

        internal NameSpacedParameter(string parameterName, string Namespace, string defaultNamespaceUri)
        {
            this.parameterName = parameterName;
            if (!string.IsNullOrEmpty(Namespace))
            {
                parameterNamespace = Namespace;
            }
            else
            {
                parameterNamespace = defaultNamespaceUri;
            }
        }

        /// <summary>
        /// Namespace for the query parameter
        /// </summary>
        /// <remarks>
        /// <para>
        /// All query parameters, even in the OpenSearch namespace, will have an non-null/non-empty value.
        /// </para>
        /// <para>
        /// Namespaces are case sensitive.
        /// </para>
        /// </remarks>
        public string Namespace
        {
            get { return parameterNamespace; }
        }

        /// <summary>
        /// Parameter name for the query parameter
        /// </summary>
        /// <remarks>
        /// <para>
        /// Parameter names are case sensitive.
        /// </para>
        /// </remarks>
        public string Parameter
        {
            get { return parameterName; }
        }
    }
}